<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      
      <el-form-item label="回款编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入回款编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="checkStatus">
          <el-select v-model="queryParams.checkStatus" placeholder="请选择审核状态" clearable size="small">
           <el-option
                v-for="dict in receivablesStatusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          plain
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['crm:receivables:add']"
        >新增回款</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['crm:receivables:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
         plain
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['crm:receivables:remove']"
        >删除</el-button>
      </el-col>
     
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="crmReceivablesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="回款编号" align="center" prop="code" />
       <el-table-column label="客户名称" align="center" prop="customerName" :show-overflow-tooltip="true">
         <template slot-scope="scope">
           <el-link @click="openCustomer(scope.row)" :underline="false" type="primary" >{{scope.row.customerName}}</el-link>
        </template>
      </el-table-column>
        <el-table-column label="合同" align="center" prop="contractName" width="150px">
         <template slot-scope="scope">
           <el-link @click="openContract(scope.row)" :underline="false" type="primary" >{{scope.row.contractName}}</el-link>
        </template>
      </el-table-column>
       <el-table-column label="审核类型" align="center" prop="checkStatus"  width="100px">
        <template slot-scope="scope">
              <el-tag prop="checkStatus" v-for="(item, index) in receivablesStatusOptions"  :type="scope.row.checkStatus==1?'success':'danger'"   v-if="scope.row.checkStatus==item.dictValue">{{item.dictLabel}}</el-tag>
          </template>
      </el-table-column>
      <el-table-column label="回款日期" align="center" prop="returnTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.returnTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="回款方式" align="center" prop="returnType"  width="100px">
        <template slot-scope="scope">
              <el-tag prop="returnType" v-for="(item, index) in receivablesTypeOptions"     v-if="scope.row.returnType==item.dictValue">{{item.dictLabel}}</el-tag>
          </template>
      </el-table-column>
      <el-table-column label="回款金额" align="center" prop="money" />
      <el-table-column label="负责人" align="center" prop="userNickName" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="审核人" align="center" prop="auditUserNickName" />
      <el-table-column label="审核时间" align="center" prop="auditTime" width="180">
      <template slot-scope="scope">
        <span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
      </template>
      </el-table-column>
      <el-table-column label="审核意见" align="center" prop="auditNote" />
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改回款对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="回款编号" prop="code">
          <el-input v-model="form.code" placeholder="请输入回款编号" />
        </el-form-item>
     
        <el-form-item label="合同" prop="contractId">
          <el-select v-model="form.contractId" @change="handleContractChange" placeholder="请选择回款方式" clearable size="small">
              <el-option
                    v-for="item in contracts"
                    :key="item.contractId"
                    :label="item.name"
                    :value="item.contractId"
                  />
            </el-select>
        </el-form-item>
        <el-form-item label="客户" prop="customerName">
          <el-input v-model="form.customerName" :disabled="true" style="width: 220px"   />
        </el-form-item>
        <el-form-item label="回款日期" prop="returnTime">
          <el-date-picker clearable size="small" style="width: 220px"
            v-model="form.returnTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择回款日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="回款方式" prop="returnType">
           <el-select v-model="form.returnType" placeholder="请选择回款方式" clearable size="small">
              <el-option
                    v-for="dict in receivablesTypeOptions"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue"
                  />
            </el-select>
        </el-form-item>
        <el-form-item label="回款金额" prop="money">
          <el-input v-model="form.money" type="number" placeholder="请输入回款金额" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-drawer
     size="50%"
     :with-header="false"
      :title="show.title" :visible.sync="show.open"
      >
        <customer  ref="customer" />
    </el-drawer>
     <el-drawer
     size="50%"
     :with-header="false"
      :title="contractShow.title" :visible.sync="contractShow.open"
      >
        <contract  ref="contract" />
    </el-drawer>
  </div>
</template>

<script>
import { listCrmReceivables, getCrmReceivables, delCrmReceivables, addCrmReceivables, updateCrmReceivables, exportCrmReceivables } from "@/api/crm/receivables";
import { getMySuccessContractList } from "@/api/crm/contract";
import customer from "../../components/customerDetails";
import contract from "../../components/contractDetails";

export default {
  name: "CrmReceivables",
  components: { customer,contract },
  data() {
    return {
      contractShow:{
        open:false,
        title:'合同详情'
      },
      show:{
        open:false,
        title:'客户详情'
      },
      contracts:[],
      receivablesTypeOptions:[],
      receivablesStatusOptions:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 回款表格数据
      crmReceivablesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planId: null,
        code: null,
        customerId: null,
        contractId: null,
        checkStatus: null,
        auditRecordId: null,
        returnTime: null,
        returnType: null,
        money: null,
        createUserId: null,
        ownerUserId: null,
        remarks: null
      },
      // 表单参数
      form: {
        customerName:undefined,
      },
      // 表单校验
      rules: {
        code: [
          { required: true, message: "回款编号不能为空", trigger: "blur" }
        ],
        contractId: [
          { required: true, message: "合同不能为空", trigger: "blur" }
        ],
         returnTime: [
          { required: true, message: "回款时间不能为空", trigger: "blur" }
        ],
         returnType: [
          { required: true, message: "回款方式不能为空", trigger: "blur" }
        ],
         money: [
          { required: true, message: "回款金额不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("receivables_status").then((response) => {
      this.receivablesStatusOptions = response.data;
    });
    this.getDicts("receivables_type").then((response) => {
      this.receivablesTypeOptions = response.data;
    });

    
  },
  methods: {
     openContract(row){
      this.contractShow.open=true;
      const contractId = row.contractId || this.ids
      setTimeout(() => {
        this.$refs.contract.getContract(contractId);
      }, 500);
    },
     openCustomer(row){
      this.show.open=true;
      const customerId = row.customerId || this.ids
      setTimeout(() => {
        this.$refs.customer.getCustomerDetails(customerId);
      }, 500);
    },
    handleContractChange(val){
      console.log(val)
      var that=this;
      this.contracts.forEach(function (item) {
          if (item.contractId == val) {
              that.form.customerName=item.customerName;
              that.form.customerId=item.customerId;
          }
         
      });
    },
    /** 查询回款列表 */
    getList() {
      this.loading = true;
      listCrmReceivables(this.queryParams).then(response => {
        this.crmReceivablesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        receivablesId: null,
        planId: null,
        code: null,
        customerId: null,
        contractId: null,
        checkStatus: 0,
        auditRecordId: null,
        returnTime: null,
        returnType: null,
        money: null,
        remark: null,
        createUserId: null,
        ownerUserId: null,
        createTime: null,
        updateTime: null,
        remarks: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.receivablesId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加回款";
      getMySuccessContractList().then(response => {
        this.contracts = response.list;
      });
    },
    // /** 修改按钮操作 */
    // handleUpdate(row) {
    //   this.reset();
    //   const receivablesId = row.receivablesId || this.ids
    //   getCrmReceivables(receivablesId).then(response => {
    //     this.form = response.data;
    //     this.open = true;
    //     this.title = "修改回款";
    //   });
    // },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
            addCrmReceivables(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const receivablesIds = row.receivablesId || this.ids;
      this.$confirm('是否确认删除回款编号为"' + receivablesIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCrmReceivables(receivablesIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有回款数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCrmReceivables(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
<style>
 
</style>
